home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / doc / interpreter / strings.tex < prev    next >
Text File  |  1997-08-13  |  15KB  |  469 lines

  1. @c Copyright (C) 1996, 1997 John W. Eaton
  2. @c This is part of the Octave manual.
  3. @c For copying conditions, see the file gpl.tex.
  4.  
  5. @node Strings, Data Structures, Numeric Data Types, Top
  6. @chapter Strings
  7. @cindex strings
  8. @cindex character strings
  9. @opindex "
  10. @opindex '
  11.  
  12. A @dfn{string constant} consists of a sequence of characters enclosed in
  13. either double-quote or single-quote marks.  For example, both of the
  14. following expressions
  15.  
  16. @example
  17. @group
  18. "parrot"
  19. 'parrot'
  20. @end group
  21. @end example
  22.  
  23. @noindent
  24. represent the string whose contents are @samp{parrot}.  Strings in
  25. Octave can be of any length.
  26.  
  27. Since the single-quote mark is also used for the transpose operator
  28. (@pxref{Arithmetic Ops}) but double-quote marks have no other purpose in
  29. Octave, it is best to use double-quote marks to denote strings.
  30.  
  31. @c XXX FIXME XXX -- this is probably pretty confusing.
  32.  
  33. @cindex escape sequence notation
  34. Some characters cannot be included literally in a string constant.  You
  35. represent them instead with @dfn{escape sequences}, which are character
  36. sequences beginning with a backslash (@samp{\}).
  37.  
  38. One use of an escape sequence is to include a double-quote
  39. (single-quote) character in a string constant that has been defined
  40. using double-quote (single-quote) marks.  Since a plain double-quote
  41. would end the string, you must use @samp{\"} to represent a single
  42. double-quote character as a part of the string.  The backslash character
  43. itself is another character that cannot be included normally.  You must
  44. write @samp{\\} to put one backslash in the string.  Thus, the string
  45. whose contents are the two characters @samp{"\} may be written
  46. @code{"\"\\"} or @code{'"\\'}.  Similarly, the string whose contents are
  47. the two characters @samp{'\} may be written @code{'\'\\'} or @code{"'\\"}.
  48.  
  49. Another use of backslash is to represent unprintable characters
  50. such as newline.  While there is nothing to stop you from writing most
  51. of these characters directly in a string constant, they may look ugly.
  52.  
  53. Here is a table of all the escape sequences used in Octave.  They are
  54. the same as those used in the C programming language.
  55.  
  56. @table @code
  57. @item \\
  58. Represents a literal backslash, @samp{\}.
  59.  
  60. @item \"
  61. Represents a literal double-quote character, @samp{"}.
  62.  
  63. @item \'
  64. Represents a literal single-quote character, @samp{'}.
  65.  
  66. @item \a
  67. Represents the ``alert'' character, control-g, ASCII code 7.
  68.  
  69. @item \b
  70. Represents a backspace, control-h, ASCII code 8.
  71.  
  72. @item \f
  73. Represents a formfeed, control-l, ASCII code 12.
  74.  
  75. @item \n
  76. Represents a newline, control-j, ASCII code 10.
  77.  
  78. @item \r
  79. Represents a carriage return, control-m, ASCII code 13.
  80.  
  81. @item \t
  82. Represents a horizontal tab, control-i, ASCII code 9.
  83.  
  84. @item \v
  85. Represents a vertical tab, control-k, ASCII code 11.
  86.  
  87. @c We don't do octal or hex this way yet.
  88. @c
  89. @c @item \@var{nnn}
  90. @c Represents the octal value @var{nnn}, where @var{nnn} are one to three
  91. @c digits between 0 and 7.  For example, the code for the ASCII ESC
  92. @c (escape) character is @samp{\033}.@refill
  93. @c 
  94. @c @item \x@var{hh}@dots{}
  95. @c Represents the hexadecimal value @var{hh}, where @var{hh} are hexadecimal
  96. @c digits (@samp{0} through @samp{9} and either @samp{A} through @samp{F} or
  97. @c @samp{a} through @samp{f}).  Like the same construct in @sc{ansi} C,
  98. @c the escape 
  99. @c sequence continues until the first non-hexadecimal digit is seen.  However,
  100. @c using more than two hexadecimal digits produces undefined results.  (The
  101. @c @samp{\x} escape sequence is not allowed in @sc{posix} @code{awk}.)@refill
  102. @end table
  103.  
  104. Strings may be concatenated using the notation for defining matrices.
  105. For example, the expression
  106.  
  107. @example
  108. [ "foo" , "bar" , "baz" ]
  109. @end example
  110.  
  111. @noindent
  112. produces the string whose contents are @samp{foobarbaz}.  @xref{Numeric
  113. Data Types} for more information about creating matrices.
  114.  
  115. @menu
  116. * Creating Strings::            
  117. * Searching and Replacing::     
  118. * String Conversions::          
  119. * Character Class Functions::   
  120. @end menu
  121.  
  122. @node Creating Strings, Searching and Replacing, Strings, Strings
  123. @section Creating Strings
  124.  
  125. @deftypefn {Function File} {} blanks (@var{n})
  126. Return a string of @var{n} blanks.
  127. @end deftypefn
  128.  
  129. @deftypefn {Function File} {} int2str (@var{n})
  130. @deftypefnx {Function File} {} num2str (@var{x})
  131. Convert a number to a string.  These functions are not very flexible,
  132. but are provided for compatibility with @sc{Matlab}.  For better control
  133. over the results, use @code{sprintf} (@pxref{Formatted Output}).
  134. @end deftypefn
  135.  
  136. @deftypefn {Built-in Function} {} setstr (@var{x})
  137. Convert a matrix to a string.  Each element of the matrix is converted
  138. to the corresponding ASCII 
  139. character.  For example,
  140.  
  141. @example
  142. @group
  143. setstr ([97, 98, 99])
  144.      @result{} "abc"
  145. @end group
  146. @end example
  147. @end deftypefn
  148.  
  149. @deftypefn {Function File} {} strcat (@var{s1}, @var{s2}, @dots{})
  150. Return a string containing all the arguments concatenated.  For example,
  151.  
  152. @example
  153. @group
  154. s = [ "ab"; "cde" ];
  155. strcat (s, s, s)
  156.      @result{} "ab ab ab "
  157.         "cdecdecde"
  158. @end group
  159. @end example
  160. @end deftypefn
  161.  
  162. @defvr {Built-in Variable} string_fill_char
  163. The value of this variable is used to pad all strings in a string matrix
  164. to the same length.  It should be a single character.  The default value
  165. is @code{" "} (a single space).  For example,
  166.  
  167. @example
  168. @group
  169. string_fill_char = "X";
  170. [ "these"; "are"; "strings" ]
  171.      @result{} "theseXX"
  172.         "areXXXX"
  173.         "strings"
  174. @end group
  175. @end example
  176. @end defvr
  177.  
  178. @deftypefn {Function File} {} str2mat (@var{s_1}, @dots{}, @var{s_n})
  179. Return a matrix containing the strings @var{s_1}, @dots{}, @var{s_n} as
  180. its rows.  Each string is padded with blanks in order to form a valid
  181. matrix.
  182.  
  183. @strong{Note:}
  184. This function is modelled after @sc{Matlab}.  In Octave, you can create
  185. a matrix of strings by @code{[@var{s_1}; @dots{}; @var{s_n}]} even if
  186. the strings are not all the same length.
  187. @end deftypefn
  188.  
  189. @deftypefn {Built-in Function} {} isstr (@var{a})
  190. Return 1 if @var{a} is a string.  Otherwise, return 0.
  191. @end deftypefn
  192.  
  193. @node Searching and Replacing, String Conversions, Creating Strings, Strings
  194. @section Searching and Replacing
  195.  
  196. @deftypefn {Function File} {} deblank (@var{s})
  197. Removes the trailing blanks from the string @var{s}. 
  198. @end deftypefn
  199.  
  200. @deftypefn {Function File} {} findstr (@var{s}, @var{t}, @var{overlap})
  201. Return the vector of all positions in the longer of the two strings
  202. @var{s} and @var{t} where an occurrence of the shorter of the two starts.
  203. If the optional argument @var{overlap} is nonzero, the returned vector
  204. can include overlapping positions (this is the default).  For example,
  205.  
  206. @example
  207. findstr ("ababab", "a")
  208.      @result{} [ 1, 3, 5 ]
  209. findstr ("abababa", "aba", 0)
  210.      @result{} [ 1, 5 ]
  211. @end example
  212. @end deftypefn
  213.  
  214. @deftypefn {Function File} {} index (@var{s}, @var{t})
  215. Return the position of the first occurrence of the string @var{t} in the
  216. string @var{s}, or 0 if no occurrence is found.  For example,
  217.  
  218. @example
  219. index ("Teststring", "t")
  220.      @result{} 4
  221. @end example
  222.  
  223. @strong{Note:}  This function does not work for arrays of strings.
  224. @end deftypefn
  225.  
  226. @deftypefn {Function File} {} rindex (@var{s}, @var{t})
  227. Return the position of the last occurrence of the string @var{t} in the
  228. string @var{s}, or 0 if no occurrence is found.  For example,
  229.  
  230. @example
  231. rindex ("Teststring", "t")
  232.      @result{} 6
  233. @end example
  234.  
  235. @strong{Note:}  This function does not work for arrays of strings.
  236. @end deftypefn
  237.  
  238. @deftypefn {Function File} {} split (@var{s}, @var{t})
  239. Divides the string @var{s} into pieces separated by @var{t}, returning
  240. the result in a string array (padded with blanks to form a valid
  241. matrix).  For example,
  242.  
  243. @example
  244. split ("Test string", "t")
  245.      @result{} "Tes "
  246.         " s  "
  247.         "ring"
  248. @end example
  249. @end deftypefn
  250.  
  251. @deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})
  252. Compares two strings, returning 1 if they are the same, and 0 otherwise.
  253.  
  254. @strong{Note:}  For compatibility with @sc{Matlab}, Octave's strcmp
  255. function returns 1 if the strings are equal, and 0 otherwise.  This is
  256. just the opposite of the corresponding C library function.
  257. @end deftypefn
  258.  
  259. @deftypefn {Function File} {} strrep (@var{s}, @var{x}, @var{y})
  260. Replaces all occurrences of the substring @var{x} of the string @var{s}
  261. with the string @var{y}.  For example,
  262.  
  263. @example
  264. strrep ("This is a test string", "is", "&%$")
  265.      @result{} "Th&%$ &%$ a test string"
  266. @end example
  267. @end deftypefn
  268.  
  269. @deftypefn {Function File} {} substr (@var{s}, @var{beg}, @var{len})
  270. Return the substring of @var{s} which starts at character number
  271. @var{beg} and is @var{len} characters long.  For example,
  272.  
  273. @example
  274. substr ("This is a test string", 6, 9)
  275.      @result{} "is a test"
  276. @end example
  277.  
  278. @quotation
  279. @strong{Note:}
  280. This function is patterned after AWK.  You can get the same result by
  281. @code{@var{s} (@var{beg} : (@var{beg} + @var{len} - 1))}.  
  282. @end quotation
  283. @end deftypefn
  284.  
  285. @node String Conversions, Character Class Functions, Searching and Replacing, Strings
  286. @section String Conversions
  287.  
  288. @deftypefn {Function File} {} bin2dec (@var{s})
  289. Return a decimal number corresponding to the the binary number
  290. represented as a string of zeros and ones.  For example,
  291.  
  292. @example
  293. bin2dec ("1110")
  294.      @result{} 14
  295. @end example
  296. @end deftypefn
  297.  
  298. @deftypefn {Function File} {} dec2bin (@var{n})
  299. Return a binary number corresponding the the nonnegative decimal number
  300. @var{n}, as a string of ones and zeros.  For example,
  301.  
  302. @example
  303. dec2bin (14)
  304.      @result{} "1110"
  305. @end example
  306. @end deftypefn
  307.  
  308. @deftypefn {Function File} {} dec2hex (@var{n})
  309. Return the hexadecimal number corresponding to the nonnegative decimal
  310. number @var{n}, as a string.  For example,
  311.  
  312. @example
  313. dec2hex (2748)
  314.      @result{} "abc"
  315. @end example
  316. @end deftypefn
  317.  
  318. @deftypefn {Function File} {} hex2dec (@var{s})
  319. Return the decimal number corresponding to the hexadecimal number stored
  320. in the string @var{s}.  For example,
  321.  
  322. @example
  323. hex2dec ("12B")
  324.      @result{} 299
  325. hex2dec ("12b")
  326.      @result{} 299
  327. @end example
  328. @end deftypefn
  329.  
  330. @deftypefn {Function File} {} str2num (@var{s})
  331. Convert the string @var{s} to a number.
  332. @end deftypefn
  333.  
  334. @deftypefn {Function File} {} toascii (@var{s})
  335. Return ASCII representation of @var{s} in a matrix.  For example,
  336.  
  337. @example
  338. @group
  339. toascii ("ASCII")
  340.      @result{} [ 65, 83, 67, 73, 73 ]
  341. @end group
  342.  
  343. @end example
  344. @end deftypefn
  345.  
  346. @deftypefn {Function File} {} tolower (@var{s})
  347. Return a copy of the string @var{s}, with each upper-case character
  348. replaced by the corresponding lower-case one; nonalphabetic characters
  349. are left unchanged.  For example,
  350.  
  351. @example
  352. tolower ("MiXeD cAsE 123")
  353.      @result{} "mixed case 123"
  354. @end example
  355. @end deftypefn
  356.  
  357. @deftypefn {Function File} {} toupper (@var{s})
  358. Return a copy of the string @var{s}, with each  lower-case character
  359. replaced by the corresponding upper-case one; nonalphabetic characters
  360. are left unchanged.  For example,
  361.  
  362. @example
  363. @group
  364. toupper ("MiXeD cAsE 123")
  365.      @result{} "MIXED CASE 123"
  366. @end group
  367. @end example
  368. @end deftypefn
  369.  
  370. @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})
  371. Converts special characters in strings back to their escaped forms.  For
  372. example, the expression
  373.  
  374. @example
  375. bell = "\a";
  376. @end example
  377.  
  378. @noindent
  379. assigns the value of the alert character (control-g, ASCII code 7) to
  380. the string variable @code{bell}.  If this string is printed, the
  381. system will ring the terminal bell (if it is possible).  This is
  382. normally the desired outcome.  However, sometimes it is useful to be
  383. able to print the original representation of the string, with the
  384. special characters replaced by their escape sequences.  For example,
  385.  
  386. @example
  387. octave:13> undo_string_escapes (bell)
  388. ans = \a
  389. @end example
  390.  
  391. @noindent
  392. replaces the unprintable alert character with its printable
  393. representation.
  394. @end deftypefn
  395.  
  396. @defvr {Built-in Variable} implicit_str_to_num_ok
  397. If the value of @code{implicit_str_to_num_ok} is nonzero, implicit
  398. conversions of strings to their numeric ASCII equivalents are allowed.
  399. Otherwise, an error message is printed and control is returned to the
  400. top level.  The default value is 0.
  401. @end defvr
  402.  
  403. @node Character Class Functions,  , String Conversions, Strings
  404. @section Character Class Functions
  405.  
  406. Octave also provides the following character class test functions
  407. patterned after the functions in the standard C library.  They all
  408. operate on string arrays and return matrices of zeros and ones.
  409. Elements that are nonzero indicate that the condition was true for the
  410. corresponding character in the string array.  For example,
  411.  
  412. @example
  413. @group
  414. isalpha ("!Q@@WERT^Y&")
  415.      @result{} [ 0, 1, 0, 1, 1, 1, 1, 0, 1, 0 ]
  416. @end group
  417. @end example
  418.  
  419. @deftypefn {Mapping Function} {} isalnum (@var{s})
  420. Return 1 for characters that are letters or digits (@code{isalpha
  421. (@var{a})} or @code{isdigit (@var{})} is true).
  422. @end deftypefn
  423.  
  424. @deftypefn {Mapping Function} {} isalpha (@var{s})
  425. Return true for characters that are letters (@code{isupper (@var{a})}
  426. or @code{islower (@var{})} is true). 
  427. @end deftypefn
  428.  
  429. @deftypefn {Mapping Function} {} isascii (@var{s})
  430. Return 1 for characters that are ASCII (in the range 0 to 127 decimal).
  431. @end deftypefn
  432.  
  433. @deftypefn {Mapping Function} {} iscntrl (@var{s})
  434. Return 1 for control characters.
  435. @end deftypefn
  436.  
  437. @deftypefn {Mapping Function} {} isdigit (@var{s})
  438. Return 1 for characters that are decimal digits.
  439. @end deftypefn
  440.  
  441. @deftypefn {Mapping Function} {} isgraph (@var{s})
  442. Return 1 for printable characters (but not the space character).
  443. @end deftypefn
  444.  
  445. @deftypefn {Mapping Function} {} islower (@var{s})
  446. Return 1 for characters that are lower case letters.
  447. @end deftypefn
  448.  
  449. @deftypefn {Mapping Function} {} isprint (@var{s})
  450. Return 1 for printable characters (including the space character).
  451. @end deftypefn
  452.  
  453. @deftypefn {Mapping Function} {} ispunct (@var{s})
  454. Return 1 for punctuation characters.
  455. @end deftypefn
  456.  
  457. @deftypefn {Mapping Function} {} isspace (@var{s})
  458. Return 1 for whitespace characters (space, formfeed, newline,
  459. carriage return, tab, and vertical tab).
  460. @end deftypefn
  461.  
  462. @deftypefn {Mapping Function} {} isupper (@var{s})
  463. Return 1 for upper case letters.
  464. @end deftypefn
  465.  
  466. @deftypefn {Mapping Function} {} isxdigit (@var{s})
  467. Return 1 for characters that are hexadecimal digits.
  468. @end deftypefn
  469.